package com.pic.mgt.haishin.action;

import org.apache.commons.lang.StringUtils;

import com.pic.mgt.haishin.manager.terminal.HaishinTerminalManager;
import com.pic.mgt.manager.terminal.BasicTerminalInfoBean;
import com.pic.mgt.manager.terminal.TerminalManagementOperationException;

public class TerminalMgtActionImpl implements TerminalMgtAction {
	
	private HaishinTerminalManager haishinTerminalManager;

	@Override
	public boolean addTerminal(String terminalId) {
		try {
			this.haishinTerminalManager.addTerminal(terminalId);
			
			return true;
		} catch (TerminalManagementOperationException e) {
			// 不用寫 Log (已寫)
			return false;
		}
	}

	@Override
	public boolean addTerminal(String terminalId, String effStartTimeStr) {
		try {
			this.haishinTerminalManager.addTerminal(terminalId, effStartTimeStr);
			
			return true;
		} catch (TerminalManagementOperationException e) {
			// 不用寫 Log (已寫)
			return false;
		}
	}

	@Override
	public boolean addTerminal(String terminalId, String effStartTimeStr,
			String effEndTimeStr) {
		try {
			this.haishinTerminalManager.addTerminal(terminalId, effStartTimeStr, effEndTimeStr);
			
			return true;
		} catch (TerminalManagementOperationException e) {
			// 不用寫 Log (已寫)
			return false;
		}
	}

	@Override
	public String checkTerminalId(String terminalId) {
		BasicTerminalInfoBean basicTerminalInfoBean =
			this.haishinTerminalManager.getBasicTerminalInfoBean(terminalId);
		
		if (null == basicTerminalInfoBean) {
			return "Terminal id '" + terminalId + "' doesn't exist in terminal management list.";
		}
		else {
			StringBuffer terminalInfoStringBuf = new StringBuffer();
			terminalInfoStringBuf.append("\r\n");
			terminalInfoStringBuf.append(StringUtils.rightPad("Terminal Id", 15));
			terminalInfoStringBuf.append(StringUtils.rightPad("Eff. Start Time", 35));
			terminalInfoStringBuf.append("Eff. End Time");
			terminalInfoStringBuf.append("\r\n");
			terminalInfoStringBuf.append("----------------------------------------------------------------------------------\r\n");
			
			terminalInfoStringBuf.append(StringUtils.rightPad(basicTerminalInfoBean.getTerminalId(),15));
			terminalInfoStringBuf.append(StringUtils.rightPad(basicTerminalInfoBean.getEffectiveStartTime().toString(),35));
			terminalInfoStringBuf.append(basicTerminalInfoBean.getEffectiveEndTime());
			
			terminalInfoStringBuf.append("\r\n");
			
			return terminalInfoStringBuf.toString();
		}
	}

	@Override
	public boolean removeTerminal(String terminalId) {
		try {
			this.haishinTerminalManager.removeTerminal(terminalId);
			
			return true;
		} catch (TerminalManagementOperationException e) {
			// 不用寫 Log (已寫)
			return false;
		}
	}
	
	// --- getter and setter ---

	public void setHaishinTerminalManager(
			HaishinTerminalManager haishinTerminalManager) {
		this.haishinTerminalManager = haishinTerminalManager;
	}

}
